package com.gui.utils.tree;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/8/19 17:54
 */
public class JTreeDemo {
    public static void main(String[] args) {
        JFrame frame = new JFrame("教师资历信息");
        frame.setSize(330, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JTreeDemo().createComponent());
        frame.pack();
        frame.setVisible(true);

    }

    private JScrollPane createComponent() {
        JPanel panel = new JPanel();
        JScrollPane sp=new JScrollPane(panel);
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("教师学历信息");
        String Teachers[][] = new String[3][];
        Teachers[0] = new String[]{"王鹏", "李曼", "韩小国", "穆保龄", "尚凌云", "范超峰"};
        Teachers[1] = new String[]{"胡会强", "张春辉", "宋芳", "阳芳", "朱山根", "张茜", "宋媛媛"};
        Teachers[2] = new String[]{"刘丹", "张小芳", "刘华亮", "聂来", "吴琼"};
        String gradeNames[] = {"硕士学历", "博士学历", "博士后学历"};
        DefaultMutableTreeNode node = null;
        DefaultMutableTreeNode childNode = null;
        int length = 0;
        for (int i = 0; i < 3; i++) {
            length = Teachers[i].length;
            node = new DefaultMutableTreeNode(gradeNames[i]);
            for (int j = 0; j < length; j++) {
                childNode = new DefaultMutableTreeNode(Teachers[i][j]);
                node.add(childNode);
            }
            root.add(node);
        }
        JTree tree = new JTree(root);
        panel.add(tree);
        panel.setVisible(true);
        sp.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println(e.getSource());
                System.out.println(e.getComponent());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        return sp;
    }
}
